package com.bbs.toutiao.service.admin.impl;


import com.bbs.toutiao.aspect.SysLog;
import com.bbs.toutiao.entity.ArticleClassifyEntity;
import com.bbs.toutiao.exception.BaseException;
import com.bbs.toutiao.exception.ExceptionEnum;
import com.bbs.toutiao.mapper.admin.ArticleMapper;
import com.bbs.toutiao.mapper.admin.UserMapper;
import com.bbs.toutiao.service.admin.ArticleService;
import com.bbs.toutiao.vo.article.ArticleClassRequestVo;
import com.bbs.toutiao.vo.article.ArticlePageRequestVo;
import com.bbs.toutiao.vo.article.ArticleVo;
import com.bbs.toutiao.vo.comm.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述: 文章操作service层
 * 作者： zhangBin
 * 时间： 2023/9/24 17:13
 * 版本： 1.0
 */

@Slf4j
@Service
public class ArticleServiceImpl implements ArticleService {
    @Resource
    ArticleMapper articleMapper;

    @Resource
    UserMapper userMapper;



    @Override
    public PageVo<ArticleClassifyEntity> findByClassPager(int page, int size) {
        //page:开始页 size:每页的数量
        Map<String, Integer> map = new HashMap<>();
        map.put("page", (page - 1) * size);
        map.put("size", size);
        List<ArticleClassifyEntity> userLimitList = articleMapper.getArticleClassifyPage(map);
        long i = articleMapper.getArticleClassCount();//数据总数
        PageVo<ArticleClassifyEntity> articleClassPage = new PageVo<>();
        articleClassPage.setPageList(userLimitList);
        articleClassPage.setTotal(i);
        return articleClassPage;
    }

    //新增分类
    @SysLog("新增分类")
    @Transactional
    @Override
    public boolean insertArticleClassify(ArticleClassRequestVo requestVo) {
        int i = articleMapper.insertClassify(requestVo);
        return i > 0;
    }

    //更新分类
    @SysLog("更新分类")
    @Transactional
    @Override
    public boolean updateArticleClassify(ArticleClassRequestVo requestVo) {
        int i = articleMapper.updateClassify(requestVo);
        return i > 0;
    }

    /**
     * 文章分页查询
     *
     * @param requestVo
     * @return
     */
    @Override
    public PageVo<ArticleVo> findByArticlePager(ArticlePageRequestVo requestVo) {
        List<ArticleVo> articleList = articleMapper.getArticlesPage(requestVo);
        for (ArticleVo ar : articleList) {
            ar.setImg("");
        }
        long i = articleMapper.getArticleCount(requestVo);//数据总数
        PageVo<ArticleVo> articlePage = new PageVo<>();
        articlePage.setPageList(articleList);
        articlePage.setTotal(i);
        return articlePage;
    }

    //根据id查找文章内容
    @Override
    public String findByIdArticle(int id) {
        String article = articleMapper.findByIdArticle(id);
        if (article == null) {
            throw new BaseException(ExceptionEnum.NOT_FOUND);
        } else {
            return article;
        }
    }

    //动态根据id更新文章状态
    @SysLog("更新文章状态")
    @Transactional
    @Override
    public int updateArticle(List<Map<String, Object>> articleVos) {
        return articleMapper.updateByIdArticle(articleVos);
    }
}
